import { defineStore } from "pinia";
import { UserCheckLoginReq } from "../http/user";
import { useProjectStore } from "./projectStore";

const key={
	token:'jz-app-token',
	user:'jz-app-user',
    projectId:'jz-app-project'
}

const projectStore=useProjectStore()

export const useUserStore = defineStore({
    id:'user',
    state:()=>({
        user:uni.getStorageSync(key.user) ||{},
        token:uni.getStorageSync(key.token) ||'',
        projectId:uni.getStorageSync(key.projectId) ||-1
    }),
    actions:{
        isLogin(){
            return this.getToken()!=""
        },
        setToken(token){
            this.token=token
			uni.setStorage({
				key:key.token,
				data:this.token
			})
        },
        isSuperAdmin(){
            return this.isLogin() && this.user.isSuperAdmin=='Y'
        },
        setUser(user){
            this.user=user
			uni.setStorage({
				key:key.user,
				data:this.user
			})
        },
        getToken(){
			this.token=uni.getStorageSync(key.token) ||''
            return this.token
        },
        getUser(){
			this.user=uni.getStorageSync(key.user) ||{}
            return this.user
        },
        logout(){
            console.log(123)
            uni.removeStorage({
            	key:key.user
            })
			uni.removeStorage({
				key:key.token
			})
            projectStore.clearProject()
			uni.reLaunch({
				url:"/pages/login"
			})
            
        }
    }
})